﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Utilities;
using KZ.Express.H.ENT;
using KZ.Express.H.Presenter;
using KZ.Express.H.BLL;

namespace KZ.Express.H.Settings.Customers
{
    public partial class CustomerDetailView : BaseView
    {
        #region Private Variables


        #endregion Private Variables

        #region Private Methods

        private void CustomerDetailView_Load(object sender, EventArgs e)
        {
            LoadDataUser();
            LoadCustomer();
            LoadData();
        }

        private void LoadDataUser()
        {
            try
            {
                int? userID = Utility.IntNParse(cbUser.SelectedValue);

                SearchCriteria search = new SearchCriteria();
                search.Add("RoleID", 2);
                if (UserENT._ROLEID == 2)
                    search.Add("UserID", UserENT._USERID);
                DataTable dt = _presenter.LoadDataTable<UserENT>(search);
                dt.Rows.InsertAt(dt.NewRow(), 0);
                cbUser.DataSource = dt;

                if (UserENT._ROLEID == 2)
                {
                    cbUser.SelectedValue = UserENT._USERID;
                    cbUser.Enabled = false;
                }

                if (userID.HasValue)
                    cbUser.SelectedValue = userID;
                else
                    cbUser.SelectedValue = DBNull.Value;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        private void LoadCustomer()
        {
            try
            {
                DataTable dt = _presenter.LoadDataTable<CustomerENT>(null);
                DataRow[] rows = dt.Select("CustomerID = " + this.EntityID);
                if (rows.Length > 0)
                    dt.Rows.Remove(rows[0]);

                AutoCompleteStringCollection acs = new AutoCompleteStringCollection();
                acs.AddRange(dt.AsEnumerable().Select(p => p.Field<string>("CustomerName")).ToArray());

                this.txtCustomerName.AutoCompleteCustomSource = acs;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        private void LoadData()
        {
            try
            {
                switch (this.ActionName)
                {
                    case "Insert":
                        break;

                    case "Update":
                        _presenter.LoadEntity<CustomerENT>();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        #endregion Private Methods

        #region Constructors

        public CustomerDetailView()
        {
            InitializeComponent();

            _presenter = new BasePresenter(this);

            this.ObjectID = (int)Objects.Customer;
        }

        #endregion Constructors

        #region Public Properties

        [Search]
        public int CustomerID
        {
            get { return base.EntityID; }
        }

        public override BaseENT Entity
        {
            get
            {
                CustomerENT customer = new CustomerENT();
                customer.CustomerID = base.EntityID;
                customer.CustomerCode = txtCustomerCode.Text;
                customer.CustomerName = txtCustomerName.Text;
                customer.InvoiceCustomerName = txtInvoiceCustomerName.Text;
                customer.Address = txtAddress.Text;

                customer.Industry = txtIndustry.Text;
                customer.Phone = txtPhone.Text;
                customer.Mobile = txtMobile.Text;

                customer.Contact = txtContact.Text;
                customer.Email = txtEmail.Text;
                customer.TaxCode = txtTaxCode.Text;

                customer.UserID = Utility.IntNParse(cbUser.SelectedValue);

                customer.CreatedBy = UserENT._USERID;
                customer.UpdatedBy = UserENT._USERID;

                customer.ValidateData();

                return customer;
            }
            set
            {
                CustomerENT customer = value == null ? new CustomerENT() : value as CustomerENT;
                txtCustomerCode.Text = customer.CustomerCode;
                txtCustomerName.Text = customer.CustomerName;
                txtInvoiceCustomerName.Text = customer.InvoiceCustomerName;
                txtAddress.Text = customer.Address;

                txtIndustry.Text = customer.Industry;
                txtPhone.Text = customer.Phone;
                txtMobile.Text = customer.Mobile;

                txtContact.Text = customer.Contact;
                txtEmail.Text = customer.Email;
                txtTaxCode.Text = customer.TaxCode;

                if (!String.IsNullOrEmpty(Utility.StringParse(customer.UserID)))
                    cbUser.SelectedValue = Utility.StringParse(customer.UserID);

            }
        }

        #endregion Public Properties

        #region Public Methods

        public override void Save()
        {
            try
            {
                _presenter.Save();
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        public override void RefreshData()
        {
            LoadDataUser();
        }

        #endregion Public Methods
    }
}
